package com.wxt.blog.service;

import com.wxt.blog.dao.BlogDao;
import com.wxt.blog.dao.UserDao;
import com.wxt.blog.entity.Blog;
import com.wxt.blog.entity.User;
import com.wxt.blog.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class BlogService {

    public void saveBlog(Blog blog) {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            blogDao.insert(blog);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("保存博客失败");
        }
    }

    public List<Blog> findAllBlogs() {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            return blogDao.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public List<Blog> findBlogsByUserId(int userId) {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            return blogDao.findByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public Blog findBlogById(int blogId) {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            Blog blog = blogDao.findById(blogId);

            System.out.println("===== DEBUG =====");
            System.out.println("查询ID: " + blogId);
            System.out.println("返回对象: " + blog);

            if (blog != null) {
                System.out.println("标题: " + blog.getTitle());
                System.out.println("作者名: " + blog.getAuthorName());

                UserDao userDao = MyBatisUtil.getMapper(UserDao.class);
                User author = userDao.findById(blog.getUserId());
                blog.setAuthorName(author.getUsername());
            }
            return blog;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void incrementViewCount(int blogId) {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            blogDao.incrementViewCount(blogId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean deleteBlog(int blogId) {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            return blogDao.delete(blogId) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateBlog(Blog blog) {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            return blogDao.update(blog) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean likeBlog(int blogId, int userId) {
        SqlSession session = MyBatisUtil.getSession();
        try {
            BlogDao blogDao = session.getMapper(BlogDao.class);

            if (blogDao.checkLikeExists(blogId, userId) > 0) {
                return false;
            }

            blogDao.addLike(blogId, userId);

            blogDao.incrementLikeCount(blogId);

            session.commit();
            return true;
        } catch (Exception e) {
            session.rollback();
            throw e;
        } finally {
            session.close();
        }
    }

    public int getLikeCount(int blogId) {
        try (SqlSession session = MyBatisUtil.getSession()) {
            return session.getMapper(BlogDao.class).getLikeCount(blogId);
        }
    }

    public int handleLike(int blogId, int userId, boolean isLike) {
        SqlSession session = MyBatisUtil.getSession();
        try {
            BlogDao dao = session.getMapper(BlogDao.class);

            if (isLike) {
                if (dao.checkLikeExists(blogId, userId) > 0) {
                    throw new RuntimeException("您已经点过赞了");
                }
                dao.addLike(blogId, userId);
            } else {
                if (dao.checkLikeExists(blogId, userId) == 0) {
                    throw new RuntimeException("您尚未点赞");
                }
                dao.removeLike(blogId, userId);
            }

            dao.updateLikeCount(blogId, isLike ? 1 : -1);
            session.commit();
            return dao.getLikeCount(blogId);
        } finally {
            session.close();
        }
    }

    public List<Blog> searchBlogs(String keyword) {
        try {
            BlogDao blogDao = MyBatisUtil.getMapper(BlogDao.class);
            return blogDao.search("%" + keyword + "%");
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}